package com.cindata

import grails.transaction.Transactional

@Transactional
class RiskWarningFlowStageConditionService
{
    ComponentService componentService

    def initFlowStageCondition(RiskWarning riskWarning, Workflow workflow)
    {
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        for (
            workflowStage in
                workflowStages)
        {
            workflowStage?.conditions?.each {
                def riskWarningFlowStageCondition = new RiskWarningFlowStageCondition()
                riskWarningFlowStageCondition.executeSequence = it?.executeSequence
                riskWarningFlowStageCondition.message = it?.message
                riskWarningFlowStageCondition.log = it?.log
                riskWarningFlowStageCondition.component = it?.component
                riskWarningFlowStageCondition.nextStage = it?.nextStage
                riskWarningFlowStageCondition.flowStage = RiskWarningFlowStage.findByRiskWarningAndStage(riskWarning, workflowStage?.stage)
                if (riskWarningFlowStageCondition.validate())
                {
                    riskWarningFlowStageCondition.save flush: true
                }
                else
                {
                    println riskWarningFlowStageCondition.errors
                }
            }
        }
    }

    def evaluate(RiskWarning riskWarning)
    {
        def map = [:]
        Boolean flag = true
        def message
        def result
        def nextStage

        def flowStage = RiskWarningFlowStage.findByRiskWarningAndStage(riskWarning, riskWarning?.stage)
        if (flowStage)
        {
            for (
                it in
                    flowStage?.conditions)
            {
                try
                {
                    if (it?.component)
                    {
                        result = componentService.evaluate it?.component, riskWarning

                        it.log = result
                        it.save flush: true

                        if (result instanceof Exception)
                        {
                            message = "联系管理员：" + result
                            flag = false
                            break
                        }
                        else if (result instanceof Boolean)
                        {
                            if (!result)
                            {
                                message = it?.component?.message
                                flag = false
                                break
                            }
                        }
                        else if (result instanceof HashMap)
                        {
                            if (!result['flag'])
                            {
                                message = it?.component?.message
                                flag = false
                                break
                            }
                            else if (result['nextStage'])
                            {
                                nextStage = result['nextStage']
                            }
                        }
                        else if (result instanceof String)
                        {
                            message = result
                            flag = false
                            break
                        }
                    }
                }
                catch (Exception e)
                {
                    it.log = e
                    it.save flush: true

                    message = "联系管理员：" + e
                    flag = false
                    break
                }
            }
        }
        else
        {
            message = "押品登记流程信息不完善，请联系管理员"
            flag = false
        }

        if (message)
        {
            map['message'] = message
        }
        if (nextStage)
        {
            map['nextStage'] = nextStage
        }
        map['flag'] = flag
        return map
    }
}